package org.scboy.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.scboy.utils.entitys.coobi.Model;

import cn.hutool.core.util.StrUtil;

public class CodeUtil {
	/**
	 * 数据补充前缀
	 * 例如：1填充到6位变为000001字符串。
	 * @param curno 当前数据
	 * @param length 要求满足长度
	 * @param fillStr 填充字符
	 * @return
	 */
	public static String getCurNo(Long curno,int length,String fillStr){
		Long temp=curno;
		StringBuilder sb=new StringBuilder(length);
		int count=0;
		while(curno/10!=0){
			curno=curno/10;
			count++;
		}
		int size=length-count-1;
		for (int i = 0; i < size; i++) {
			sb.append(fillStr);
		}
		sb.append(temp);
		return sb.toString();
	}
	
	public static String getCur(String curno,int length,String fillStr){
		if(curno==null) {
			return null;
		}
		int count=curno.length();
		if(count>=length) {
			return curno.substring(0,length);
		}else {
			StringBuilder sb=new StringBuilder(curno);
			int size=length-count;
			for (int i = 0; i < size; i++) {
				sb.append(fillStr);
			}
			return sb.toString();
		}
	}
	/**
	 * 参数转义
	 * @param sql
	 * @param paramsMap
	 * @return
	 */
	public static String getMsg(String msgTitle, Model model) {
		String regex = "(@([\\w\\_]+))";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(msgTitle);
		String msgResult=msgTitle;
		while (m.find()) {
			String  msgValue=model.getString(m.group(2));
			if(StrUtil.isEmpty(msgValue)) {
				msgValue="";
			}
			msgResult=msgResult.replace(m.group(1),msgValue);
		}
		return msgResult;
	}
}
